var currentpath = document.location.pathname;
currentpath = currentpath.split("/");
var popupActionPath = "/"+currentpath[1]+"/popup/";
var lookup_width = 1000;
var lookup_height = 580;
var left = (screen.width/2)-(1000/2);
var top = (screen.height/2)-(500/2);
var dialogWin = new Object();

//for hide screen on unload action and close dialogWin
window.onbeforeunload = function() {
    div_item = document.getElementById("template_div_body");
    if (div_item) {
        div_item.style.display = "none";
    }
    if (dialogWin.win && typeof(dialogWin.win.closed) != 'unknown' && !dialogWin.win.closed)
    {
        dialogWin.win.close();
    }
};

function sendBackArrVal(strValue,txtBoxName){
        
    var arrTxt = txtBoxName.split("$");
    var arrVal = strValue.split("$");
    for(var i=0;i<arrTxt.length;i++){             	 
    	 if( arrTxt[i] != '' ){
         if(typeof(window.opener.parent.document.forms[0])!="undefined"){
            eval("window.opener.parent.document.forms[0].elements['"+arrTxt[i]+"'].value=unescape(arrVal[i])");
         }
         if(typeof(window.opener.document.forms[0])!="undefined"){
            eval("window.opener.document.forms[0].elements['"+arrTxt[i]+"'].value=unescape(arrVal[i])");
         }
        }
    }
    try {
    
     window.close();
    } catch(e) {};	             
}

function popupReportViewer()
{
	var url = "/"+currentpath[1]+"/common/jsp/cereport/ReportViewer.jsp";
	var w = openDialog(url, "ReportViewer", lookup_width, lookup_height, "toolbar=no,resizable=yes,width=10,height=10,scrollbars=1");	
}

function popupCustomerID(txtbox,callBackRef)
{
	var url = popupActionPath + "PopupInquiryCustomerIDAction.do?txtBox="+txtbox;
	var w = openDialog(url, "popupCustomerID", lookup_width, lookup_height, "resizable=yes,scrollbars=1,status=yes",callBackRef);	
}

function popupCustomerIDDup(customerCode,txtbox,callBackRef)
{
	var url = popupActionPath + "PopupInquiryCustomerIDAction_inquiry.do?key="+customerCode+"&txtBox="+txtbox;
	var w = openDialog(url, "popupCustomerID", lookup_width, lookup_height, "resizable=yes,scrollbars=1,status=yes",callBackRef);	
}

function popupCustomerIDDupByCardID(cardID,txtbox,callBackRef)
{
	var url = popupActionPath + "PopupInquiryCustomerIDAction_inquiry.do?key2="+cardID+"&txtBox="+txtbox;
	var w = openDialog(url, "popupCustomerID", lookup_width, lookup_height, "resizable=yes,scrollbars=1,status=yes",callBackRef);	
}

function popupCustomerIDDupByContractNo(contractNo,txtbox,callBackRef)
{
	var url = popupActionPath + "PopupInquiryCustomerIDAction_inquiry.do?key3="+contractNo+"&txtBox="+txtbox;
	var w = openDialog(url, "popupCustomerID", lookup_width, lookup_height, "resizable=yes,scrollbars=1,status=yes",callBackRef);	
}

function popupUserID(txtbox,callBackRef)
{
	var url = popupActionPath + "PopupInquiryUserIDAction.do?txtBox="+txtbox;
	var w = openDialog(url, "popupUserID", lookup_width, lookup_height, "resizable=yes,scrollbars=1,status=yes",callBackRef);	
}

function popupUserIDDup(userID,txtbox,callBackRef)
{
	var url = popupActionPath + "PopupInquiryUserIDAction_inquiry.do?key="+userID+"&txtBox="+txtbox;
	var w = openDialog(url, "popupUserID", lookup_width, lookup_height, "resizable=yes,scrollbars=1,status=yes",callBackRef);	
}

function popupBank(txtbox,callBackRef)
{
	var url = popupActionPath + "PopupBankAction_inquiry.do?txtBox="+txtbox;
	var w = openDialog(url, "popupBank", 900, 380, "resizable=yes,scrollbars=1,status=yes",callBackRef);	
}

function popupBankBranch(bankCode,txtbox,callBackRef)
{
	if(bankCode != '')
	{
		var url = popupActionPath + "PopupBankBranchAction_inquiry.do?txtBox="+txtbox+"&txtBankCode="+bankCode;
		var w = openDialog(url, "popupBankBranch", 900, 380, "resizable=yes,scrollbars=1,status=yes",callBackRef);
	}
}

function popupAccessLogUserProfile(uspId,uspUserName)
{
	var url = popupActionPath + "accessLogUserProfilePopup-initAccessLog.htm?uspId="+uspId+"&uspUserName="+uspUserName;
	//var w = openDialog(url, "popupAccessLogUserProfile", 1000, 500, "resizable=yes,scrollbars=1,status=yes");
	window.open(popupActionPath + "accessLogUserProfilePopup-initAccessLog.htm?uspId="+uspId+"&uspUserName="+uspUserName, "popupAccessLogUserProfile", "resizable=yes,scrollbars=1,status=yes,width=1000,height=500,top="+top+", left="+left);
}

function popupAuditLog(qsMatchSetId)
{
	var url = popupActionPath + "auditLogPopup-initAuditLog.htm?qsMatchSetId="+qsMatchSetId;
	//var w = openDialog(url, "popupAuditLog", 1000, 500, "resizable=yes,scrollbars=1,status=yes");
	window.open(popupActionPath + "auditLogPopup-initAuditLog.htm?qsMatchSetId="+qsMatchSetId, "popupAuditLog", "resizable=yes,scrollbars=1,status=yes,width=1000,height=500,top="+top+", left="+left);

}

function popupBblAuditLog(groupId)
{
	var url = popupActionPath + "bblAuditLogPopup-initBblAuditLog.htm?groupId="+groupId;
	//var w = openDialog(url, "popupBblAuditLog", 1000, 500, "resizable=yes,scrollbars=1,status=yes");
	window.open(popupActionPath + "bblAuditLogPopup-initBblAuditLog.htm?groupId="+groupId, "popupBblAuditLog", "resizable=yes,scrollbars=1,status=yes,width=1000,height=500,top="+top+", left="+left);
	
}

function popupEditCustomerContact(recordId)
{
	var url = popupActionPath + "customerContactPopup-initCustomerContact.htm?recordId="+recordId;
	//var w = openDialog(url, "popupEditCustomerContact", 1000, 500, "resizable=yes,scrollbars=1,status=yes");
	window.open(popupActionPath + "customerContactPopup-initCustomerContact.htm?recordId="+recordId, "popupEditCustomerContact", "resizable=yes,scrollbars=1,status=yes,width=1000,height=500,top="+top+", left="+left);
}

function popupViewLogResultMasterCustomer(qsmatchsetid)
{
	
	var url = popupActionPath + "logResultMasterCustomerPopup-viewLog.htm?qsmatchsetid="+qsmatchsetid;
	//var w = openDialog(url, "popupViewLogResultMasterCustomer", 1000, 500, "resizable=yes,scrollbars=1,status=yes");
	window.open(popupActionPath + "logResultMasterCustomerPopup-viewLog.htm?qsmatchsetid="+qsmatchsetid, "popupViewLogResultMasterCustomer", "resizable=yes,scrollbars=1,status=yes,width=1000,height=500,top="+top+", left="+left);
	
}

//Add Customer ViewId by thippawan @20100909
function popupViewId(recordId)
{
	
	var url = popupActionPath + "customerViewIdPopup-viewId.htm?recordId="+recordId;
	//var w = openDialog(url, "customerViewIdPopup", 1000, 500, "resizable=yes,scrollbars=1,status=yes");
	window.open(popupActionPath + "customerViewIdPopup-viewId.htm?recordId="+recordId, "customerViewIdPopup", "resizable=yes,scrollbars=1,status=yes,width=1000,height=500,top="+top+", left="+left);

}
//Add Customer ViewC2A by pimonrath @20101008
function popupViewC2A(custNo)
{
	
	var url = popupActionPath + "customerViewC2APopup-init.htm?custNo="+custNo;
	window.open(popupActionPath + "customerViewC2APopup-init.htm?custNo="+custNo, "customerViewC2APopup", "resizable=yes,scrollbars=1,status=yes,width=1000,height=500,top="+top+", left="+left);
	
}
//Add Customer ViewRM by pimonrath @20101008
function popupViewRM(custNo, system)
{
	
	var url = popupActionPath + "customerViewRMPopup-init.htm?custNo="+custNo+"&&system="+system;
	window.open(popupActionPath + "customerViewRMPopup-init.htm?custNo="+custNo+"&&system="+system, "customerViewRMPopup", "resizable=yes,scrollbars=1,status=yes,width=1000,height=500,top="+top+", left="+left);
								   
}
//Add search External Reference by pimonrath @20101014
function popupSearchExternalRef()
{
	
	var url = popupActionPath + "externalReferencePopup-init.htm";
	window.open(popupActionPath + "externalReferencePopup-init.htm", "externalReferencePopup", "resizable=yes,scrollbars=1,status=yes,width=1000,height=500,top="+top+", left="+left);
	
}

function testpop()
{
	var url = "/"+currentpath[1]+"/page/approver/test.jsp";
	alert(url);
	//openDialog(url, "test", lookup_width, lookup_height, "toolbar=no,resizable=yes,width=10,height=10,scrollbars=1");
	window.open(url, "test", "resizable=yes,scrollbars=1,status=yes,width=1000,height=500,top="+top+", left="+left);
}

function openDialog(url, name, width, height, args, callBackFunctionRef)
{
	/*
    div_item = document.getElementById("contentArea");
    if(div_item)
    	try{ div_item.setCapture(); }catch(e){}
    */ 
    if(!args)
    	args = '';

    if(!dialogWin)
    	dialogWin = new Object();
    
    //if (callBackFunctionRef){
    //    dialogWin.callBack = callBackFunctionRef;
    //    dialogWin.timer = setInterval(processDialogCallBack, 1000);
    //}
/*
    var left=null, top=null;
    if (!dialogWin.win || (dialogWin.win && dialogWin.win.closed))
    {
    */
        if (document.layers)
        {
            left = window.screenX + ((window.outerWidth - width) / 2);
            top = window.screenY + ((window.outerHeight - height) / 2);
            args = args + ";" + "dialogLeft:" + left + ";dialogTop:" + top + ";dialogWidth:" + width + "px;dialogHeight:" + height+"px";
        }
        else
        {
            //left = (screen.width - width) / 2;
            //top = (screen.height - height) / 2;
            //args = args + "," + "left=" + left + ",top=" + top + ",width=" + width + ",height=" + height;
            //args = args + "," + "dialogLeft:" + left + ";dialogTop:" + top + ";dialogWidth:" + width + "px;dialogHeight:" + height+"px";
            args = args + ";" + "center:yes;dialogWidth:" + width + "px;dialogHeight:" + height+"px";
        }
        
        dialogWin.win = window.showModalDialog(url, window, args);
        //dialogWin.win = window.open(url, name, args);
        /*
        if(div_item){
        div_item.onmousedown = function() {
            return !checkModal();
        };
        div_item.oncontextmenu = function() {
            return !checkModal();
        };
        }
        dialogWin.win.focus();
    }
    else
    {
        dialogWin.win.focus();
    }*/
    //if (callBackFunctionRef){
        dialogWin.callBack = callBackFunctionRef;
    //    dialogWin.timer = setInterval(processDialogCallBack, 500);
    //}
        if (dialogWin.callBack) {
            try {
                window.status = 'Starting invoke Callback,Please wait';
            } catch(e){}
            if (typeof(dialogWin.callBack) == 'function') {
                dialogWin.callBack();
            } else {
                eval(dialogWin.callBack);
            }
            try {
                window.status = 'Done';
            } catch(e){}
            dialogWin.callBack = null;
            //div_item = document.getElementById("body");
            //div_item.releaseCapture();
        }
    return dialogWin;
}

function processDialogCallBack() {
    if (dialogWin.win && dialogWin.win.closed) {
        clearInterval(dialogWin.timer);
        dialogWin.timer = null;
        if (dialogWin.callBack) {
            try {
                window.status = 'Starting invoke Callback,Please wait'
            } catch(e){}
            if (typeof(dialogWin.callBack) == 'function') {
                dialogWin.callBack();
            } else {
                eval(dialogWin.callBack);
            }
            try {
                window.status = 'Done'
            } catch(e){}
            dialogWin.callBack = null;
            div_item = document.getElementById("template_div_body");
            div_item.releaseCapture();
        }
    }
}

function getJSESSIONID() {
    var allCookies = document.cookie;
    var name = "JSESSIONID";
    var start = allCookies.indexOf(name + '=');
    start = start + name.length + 1;
    var temp = allCookies.substring(start, allCookies.length - 1);
    var end = temp.indexOf(';');
    var cookieValue = "";
    if (end < 0) {
        cookieValue = temp;
    } else {
        cookieValue = temp.substring(0, end);
    }
    return cookieValue;
}

function doPrintSlipProcess(callBack) {
    var jsessionID = getJSESSIONID().replace(/(\.)/g, "");
    var reportWinName = "reportWin_" + jsessionID;

    var currentpath = document.location.pathname;
    var currentpaths = currentpath.split("/");
    var contextPath = '/' + currentpaths[1];
        window.div_item = document.getElementById("template_div_body");    
    if (callBack) {
        window.printCallback = callBack;
    }
    alert('AAAAAA');
    var report_progress = openDialog(contextPath + "/common/jsp/cereport/PrintSlip-PS.jsp", reportWinName, 0, 0, "toolbar=no,resizable=no,width=0,height=0,scrollbars=no");
    var report_progress2 = openDialog(contextPath + "/common/jsp/cereport/ReportViewer.jsp", reportWinName, 800, 600, "toolbar=no,resizable=no,width=800,height=600,scrollbars=no");

}

function checkModal()
{
    if (dialogWin.win && typeof(dialogWin.win.closed) != 'unknown' && !dialogWin.win.closed)
    {
        try {
            div_item = document.getElementById("template_div_body");
            div_item.setCapture();
            dialogWin.win.document.focus();
        } catch(e) {
        }
        return true;
    } else {
        try {
            if (!dialogWin.callBack) {
                div_item = document.getElementById("template_div_body");
                div_item.releaseCapture();
            }
        } catch(e) {
        }
        return false;
    }
}

var saveClass;
function hoverRowAction(obj)
{
    saveClass = obj.className;
    obj.className = 'content-table-row-over';
}

function outRowAction(obj) {
    if (saveClass == 'content-table-row1')
        return obj.className = 'content-table-row1';
    else
        return obj.className = 'content-table-row2';
}
